using System;
using System.Collections;
using System.Xml.Serialization;

namespace com.exceptionalsoftware.spacetime.KML
{
    [Serializable()]
	public class CoordinateList
	{
		private ArrayList coords;

        public CoordinateList()
		{
            this.coords = new ArrayList();
		}

        public CoordinateList(Coordinate[] coordinates)
        {
            this.coords.AddRange(coordinates);
        }

        public CoordinateList(string coordinateString) : this()
        {
            this.initialize(coordinateString);
        }

        private void initialize(string coordinateString)
        {
            char[] separators = new char[] { ' ', '\f', '\n', '\r', '\t', '\v' };
            string[] components = coordinateString.Split(separators, StringSplitOptions.RemoveEmptyEntries);

            this.coords.Clear();
            foreach (string component in components)
            {
                this.coords.Add(new Coordinate(component));
            }
        }

        [XmlText(DataType = "string")]
        public string Serialized
        {
            set { this.initialize(value); }
            get { return this.ToString(); }
        }

        [XmlIgnore()]
        public int Count
        {
            get { return this.coords.Count; }
        }

        [XmlIgnore()]
        public Coordinate this[int index]
        {
            get { return (Coordinate)this.coords[index]; }
        }

        public void Clear()
        {
            this.coords.Clear();
        }

        public void Add(Coordinate c)
        {
            this.coords.Add(c);
        }

        public void Remove(Coordinate c)
        {
            this.coords.Remove(c);
        }

        public override string ToString()
        {
            Coordinate c = (Coordinate)this.coords[0];
            if (c == null)
            {
                return "";
            }

            string output = c.Serialized;
            for (int i = 1; i < this.coords.Count; i++)
            {
                output += "  " + ((Coordinate)this.coords[i]).Serialized;
            }
            return output;
        }

        public Coordinate[] ToArray()
        {
            return (Coordinate[])this.coords.ToArray(typeof(Coordinate));
        }
    }
}